package com.rk.kbk.controller;

import com.rk.kbk.common.JsonResp;
import com.rk.kbk.common.PubLib;
import com.rk.kbk.dao.AddressDao;
import com.rk.kbk.dao.SendRatesDao;
import com.rk.kbk.dto.PageDto;
import com.rk.kbk.model.Message;
import com.rk.kbk.model.Order;
import com.rk.kbk.model.SendRates;

import org.apache.log4j.Logger;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by bilibili on 2017/3/17.
 */

@CrossOrigin
@RestController
@Transactional
@RequestMapping(value="/api/sendRates")
public class SendRatesController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private SendRatesDao sendRatesDao;
    @PersistenceContext
    private EntityManager entityManager;



    /**
     * @添加配送费
     * @param sendRates
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/addOrUpdateSendRates",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp addOrUpdateSendRates(@RequestBody List<SendRates> sendRates){
        log.debug("添加配送费");
        return JsonResp.ok(sendRatesDao.save(sendRates));
    }

    /**
     * @分页查询(原生sql)
     * @param pageNum 第几页
     * @param pageSize 每页的条数
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/jpa/page",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp findPageAccountJpa(Integer pageNum, Integer pageSize,String areaCode) {
        log.debug("分页查询(原生sql)");
        String sql = " FROM send_rates WHERE area_code= "+areaCode+" ";

        Query query = entityManager.createNativeQuery("SELECT * " + sql);

        BigInteger count_number = null;
        //总记录数
        Query query_number = entityManager.createNativeQuery("SELECT COUNT(1) " + sql);
        count_number = (BigInteger) query_number.getSingleResult();
        long total = count_number.longValue();
        //分页
        if ((pageNum-1) >= 0) {
            query.setFirstResult((pageNum-1) * pageSize);
        }
        if (pageSize > 0) {
            query.setMaxResults(pageSize);
        }
        entityManager.close();

        query.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List<Map> mapList = new ArrayList<>();
        query.getResultList().forEach(n -> mapList.add(PubLib.copyObject(PubLib.convert(n), Map.class)));

        return JsonResp.ok(new PageDto(pageNum, pageSize, mapList, total));
    }

    /**
     * @删除配送费设置
     * @param id 编号
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/deleteSetting",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp deleteSettingOne(Long id) {
        log.debug("删除配送费设置");
        sendRatesDao.delete(id);
        return JsonResp.ok();
    }

    /**
     * @配送费信息
     * @param id 编号
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/settingDetail",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp findById(Long id) {
        log.debug("配送费信息");
        return JsonResp.ok(sendRatesDao.findOne(id));
    }

}
